{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c73d803c-a005-496f-9812-7eb414ce6449",
   "metadata": {},
   "source": [
    "# Vector Field\n",
    "\n",
    "A vector field plot plot of a synthetic dataset simulating wind speed and direction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac5ea091-6484-4fc8-9852-d48acd90cc74",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.xarray  # noqa\n",
    "import numpy as np\n",
    "import xarray as xr\n",
    "\n",
    "def sample_data(shape=(20, 30)):\n",
    "    x = np.linspace(311.9, 391.1, shape[1])\n",
    "    y = np.linspace(-23.6, 24.8, shape[0])\n",
    "    x2d, y2d = np.meshgrid(x, y)\n",
    "    u = 10 * (2 * np.cos(2 * np.deg2rad(x2d) + 3 * np.deg2rad(y2d + 30)) ** 2)\n",
    "    v = 20 * np.cos(6 * np.deg2rad(x2d))\n",
    "    return x, y, u, v\n",
    "\n",
    "xs, ys, U, V = sample_data()\n",
    "mag = np.sqrt(U**2 + V**2)\n",
    "angle = (np.pi/2.) - np.arctan2(U/mag, V/mag)\n",
    "ds = xr.Dataset({\n",
    "    'mag': xr.DataArray(mag, dims=('y', 'x'), coords={'y': ys, 'x': xs}),\n",
    "    'angle': xr.DataArray(angle, dims=('y', 'x'), coords={'y': ys, 'x': xs})\n",
    "})\n",
    "\n",
    "ds.hvplot.vectorfield(\n",
    "    x='x',\n",
    "    y='y',\n",
    "    angle='angle',\n",
    "    mag='mag',\n",
    "    color='mag',\n",
    "    cmap='viridis',\n",
    "    colorbar=True,\n",
    "    title='Vector Field (Bokeh)',\n",
    ").opts(magnitude='mag')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f0cd955-3e03-42a5-b581-f828036e1716",
   "metadata": {},
   "source": [
    ":::{seealso}\n",
    "- [Vector Field Plots reference documentation](../../ref/api/manual/hvplot.hvPlot.vectorfield.ipynb).\n",
    ":::"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
